﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Linq;

namespace Flare.DB
{
    class Source
    {
        public static SqlCeConnection connection = new SqlCeConnection(glob.DBConnection);
        public static IEnumerable<CeEntity.contract_list> contract_list()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [contract_list]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.contract_list
                                         {
                                             id_contract = Convert.ToInt32(current_record["id_contract"]),
                                             id_department = Convert.ToInt32(current_record["id_department"]),
                                             id_provider = Convert.ToInt32(current_record["id_provider"]),
                                             id_service = Convert.ToInt32(current_record["id_service"]),
                                             contract_number = current_record["contract_number"].ToString(),
                                             contract_description = current_record["contract_description"].ToString(),
                                             isclosed = Convert.ToBoolean(current_record["isclosed"])
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
        public static IEnumerable<CeEntity.contract_provider> contract_provider()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [contract_provider]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.contract_provider
                                         {
                                             id_provider = Convert.ToInt32(current_record["id_provider"]),
                                             provider = current_record["provider"].ToString(),
                                             provider_address = current_record["provider_address"].ToString(),
                                             default_contract_number = current_record["default_contract_number"].ToString()
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
        public static IEnumerable<CeEntity.contract_providerphone> contract_providerphone()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [contract_providerphone]", connection))
            {

                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.contract_providerphone
                                         {
                                             id_providerphone = Convert.ToInt32(current_record["id_providerphone"]),
                                             id_provider = Convert.ToInt32(current_record["id_provider"]),
                                             providerphone = current_record["providerphone"].ToString(),
                                             description = current_record["description"].ToString()
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
        public static IEnumerable<CeEntity.contract_service> contract_service()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [contract_service]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.contract_service
                                         {
                                             id_service = Convert.ToInt32(current_record["id_service"]),
                                             service = current_record["service"].ToString(),
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }

        public static IEnumerable<CeEntity.equipment_type> equipment_type()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [equipment_type]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.equipment_type
                                         {
                                             id_type = Convert.ToInt32(current_record["id_type"]),
                                             type = current_record["type"].ToString(),
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
        public static IEnumerable<CeEntity.equipment_mark> equipment_mark()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [equipment_mark]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.equipment_mark
                                         {
                                             id_mark = Convert.ToInt32(current_record["id_mark"]),
                                             id_type = Convert.ToInt32(current_record["id_type"]),
                                             mark = current_record["mark"].ToString(),
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
        public static IEnumerable<CeEntity.equipment_model> equipment_model()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [equipment_model]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.equipment_model
                                         {
                                             id_model = Convert.ToInt32(current_record["id_model"]),
                                             id_mark = Convert.ToInt32(current_record["id_mark"]),
                                             model = current_record["model"].ToString(),
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
        public static IEnumerable<CeEntity.equipments> equipments()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [equipments]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.equipments
                                         {
                                             id_equipment = Convert.ToInt32(current_record["id_equipment"]),
                                             id_model = Convert.ToInt32(current_record["id_model"]),
                                             id_room = Convert.ToInt32(current_record["id_room"]),
                                             inventory_number = current_record["inventory_number"].ToString(),
                                             factory_number = current_record["factory_number"].ToString(),
                                             date_create = Convert.ToDateTime(current_record["date_create"]),
                                             description = current_record["description"].ToString()
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
        public static IEnumerable<CeEntity.equipments_move_history> equipments_move_history()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [equipments_move_history]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.equipments_move_history
                                         {
                                             id_move = Convert.ToInt32(current_record["id_move"]),
                                             id_equipment = Convert.ToInt32(current_record["id_equipment"]),
                                             from_room = current_record["from_room"].ToString(),
                                             to_room = current_record["to_room"].ToString(),
                                             move_date = Convert.ToDateTime(current_record["move_date"]),
                                             description = current_record["description"].ToString()
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }

        public static IEnumerable<CeEntity.endpoints> endpoints()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [endpoints]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.endpoints
                                         {
                                             id_endpoint = Convert.ToInt32(current_record["id_endpoint"]),
                                             endpoint = current_record["endpoint"].ToString(),
                                             id_type = Convert.ToInt32(current_record["id_type"]),
                                             id_room = Convert.ToInt32(current_record["id_room"]),
                                             net_ip = current_record["net_ip"].ToString(),
                                             phone = current_record["phone"].ToString()
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
        public static IEnumerable<CeEntity.endpoint_types> endpoint_types()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [endpoint_types]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.endpoint_types
                                         {
                                             id_type = Convert.ToInt32(current_record["id_type"]),
                                             type = current_record["type"].ToString()
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }

        public static IEnumerable<CeEntity.general_department> general_department()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [general_department]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.general_department
                                         {
                                             id_department = Convert.ToInt32(current_record["id_department"]),
                                             department = current_record["department"].ToString(),
                                             department_address = current_record["department_address"].ToString(),
                                             department_contact = current_record["department_contact"].ToString(),
                                             department_contact_phone = current_record["department_contact_phone"].ToString()
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
        public static IEnumerable<CeEntity.general_room> general_room()
        {
            using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM [general_room]", connection))
            {
                try
                {
                    return command.ExecuteReader().Cast<IDataRecord>()
                                         .Select(current_record => new CeEntity.general_room
                                         {
                                             id_room = Convert.ToInt32(current_record["id_room"]),
                                             id_department = Convert.ToInt32(current_record["id_department"]),
                                             room = current_record["room"].ToString()
                                         });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(command.CommandText);
                    glob.kernel.Panic(glob.PanicType.sql, ex);
                }
                return null;
            }
        }
    }
}
